package com.facebook.android;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SinglePlay extends Activity {
	
	private TextView question;
	private Facebook facebook;
	private String myID;
	private String app_id;
	private int score;
	
	public void onCreate(Bundle savedInstanceState) {
	     super.onCreate(savedInstanceState);
	     
	     // Getting the Bundle with the information from the Intent
	     Bundle stuff = this.getIntent().getExtras();
	     // Getting the APP_ID
	     app_id = stuff.getString("api");
	     String[] qs = stuff.getStringArray("questions");
	     // make a queue for the array elements. Easier to poll()
	     Queue<String> queue = new LinkedList<String>();
	     for(int i = 0; i < qs.length; i++){
	    	 queue.add(qs[i]);
	     }
	     myID = stuff.getString("myID");
	     score = stuff.getInt("score");
	     facebook = new Facebook(app_id);
	     // Restoring the session by obtaining the session key
	     SessionStore.restore(facebook, this);
	     
	     setContentView(R.layout.singleplay);
	     
	     question = (TextView) SinglePlay.this.findViewById(R.id.question);
	     question.setTextSize(22);
	     
	     // get one of the questions
	     // size of queue -= 1
	     String takeOne = queue.poll();
	     String number = new Integer(10-queue.size()).toString();
	     
	     String domain = "http://www.pennquiz.com/users.php";
	     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	     nameValuePairs.add(new BasicNameValuePair("funct","getOneQ"));
	     nameValuePairs.add(new BasicNameValuePair("question", takeOne));
	     RequestPoster rp = new RequestPoster(domain, nameValuePairs);
	     
	     LinkedList<NameValuePair> theQuestion = rp.getQuestion();
	     
	     String q = new String();
	     String a = new String();
	     String b = new String();
	     String c = new String();
	     String d = new String();
	     String corr = new String();
	     
	     for(Iterator<NameValuePair> it = theQuestion.iterator(); it.hasNext(); ){
	    	 NameValuePair next = it.next();
	    	 if(next.getName().equals("q"))
	    		 q = next.getValue();
	    	 if(next.getName().equals("a"))
	    		 a = next.getValue();
	    	 if(next.getName().equals("b"))
	    		 b = next.getValue();
	    	 if(next.getName().equals("c"))
	    		 c = next.getValue();
	    	 if(next.getName().equals("d"))
	    		 d = next.getValue();
	    	 if(next.getName().equals("correct"))
	    		 corr = next.getValue();
	    	 else
	    		 continue;
	     }
	     String q1 = "Question "+number+"\n\n " +
	     		q + "\n\n" +
	     		"A) "+a+"\n" +
	     		"B) "+b+"\n" +
	     		"C) "+c+"\n" +
	     		"D) "+d+"\n" +
	     		"\nTotal score is " + String.valueOf(score);
	     
	     question.setText(q1);
	     
	     Button ansA = (Button) findViewById(R.id.answerA);
	     Button ansB = (Button) findViewById(R.id.answerB);
	     Button ansC = (Button) findViewById(R.id.answerC);
	     Button ansD = (Button) findViewById(R.id.answerD);
	     
	     final String correct = corr;
	     final String[] rest = new String[queue.size()];
	     int i = 0;
	     for(Iterator<String> it = queue.iterator(); it.hasNext(); ){
	    	 String next = it.next();
	    	 rest[i] = next;
	    	 i++;
	     }
	     
	     ansA.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final ProgressDialog progress = ProgressDialog.show(
						SinglePlay.this, "", "Checking answer...", true);
				
				if(correct.equals("a")){
					score = score + 5;
				}
				
				final Context c = v.getContext();
	            
			    final Intent intent = new Intent(c, SinglePlay.class);
			    final Intent safe = new Intent(c, Score.class);
	            // I am sending the APP_ID and the friends 
			    // on the other side to connect both
			    // and the userID
	            intent.putExtra("api", app_id);
	            intent.putExtra("questions", rest);
	            intent.putExtra("myID", myID);
	            intent.putExtra("score", score);
	            
	            safe.putExtra("api", app_id);
	            safe.putExtra("myID", myID);
	            safe.putExtra("score", score);
	            // Put the dialog in a new thread so that it can close 
	            // properly afterwards
	            new Thread(new Runnable(){
	            	public void run(){
	            		finish();
	            		if(rest.length > 0)
	            			startActivity(intent);
	            		else{
	            			startActivity(safe);
	            		}
	            		progress.dismiss();
	            	}
	            	}).start();
			}
	     });
	     
	     ansB.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Begin the loading dialog
				final ProgressDialog progress = ProgressDialog.show(
						SinglePlay.this, "", "Checking answer...", true);
				
				if(correct.equals("b")){
					score = score + 5;
				}
				
				final Context c = v.getContext();
	            
			    final Intent intent = new Intent(c, SinglePlay.class);
			    final Intent safe = new Intent(c, Score.class);
	            // I am sending the APP_ID and the friends 
			    // on the other side to connect both
			    // and the userID
	            intent.putExtra("api", app_id);
	            intent.putExtra("questions", rest);
	            intent.putExtra("myID", myID);
	            intent.putExtra("score", score);
	            
	            safe.putExtra("api", app_id);
	            safe.putExtra("myID", myID);
	            safe.putExtra("score", score);
	            // Put the dialog in a new thread so that it can close 
	            // properly afterwards
	            new Thread(new Runnable(){
	            	public void run(){
	            		finish();
	            		if(rest.length > 0)
	            			startActivity(intent);
	            		else{
	            			startActivity(safe);
	            		}
	            		progress.dismiss();
	            	}
	            	}).start();
			}
	    	 
	     });
	     
	     ansC.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final ProgressDialog progress = ProgressDialog.show(
						SinglePlay.this, "", "Checking answer...", true);
				
				if(correct.equals("c")){
					score = score + 5;
				}
				
				final Context c = v.getContext();
	            
			    final Intent intent = new Intent(c, SinglePlay.class);
			    final Intent safe = new Intent(c, Score.class);
	            // I am sending the APP_ID and the friends 
			    // on the other side to connect both
			    // and the userID
	            intent.putExtra("api", app_id);
	            intent.putExtra("questions", rest);
	            intent.putExtra("myID", myID);
	            intent.putExtra("score", score);
	            
	            safe.putExtra("api", app_id);
	            safe.putExtra("myID", myID);
	            safe.putExtra("score", score);
	            // Put the dialog in a new thread so that it can close 
	            // properly afterwards
	            new Thread(new Runnable(){
	            	public void run(){
	            		finish();
	            		if(rest.length > 0)
	            			startActivity(intent);
	            		else{
	            			startActivity(safe);
	            		}
	            		progress.dismiss();
	            	}
	            	}).start();
			}
	    	
	     });
	     
	     ansD.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final ProgressDialog progress = ProgressDialog.show(
						SinglePlay.this, "", "Checking answer...", true);
				
				if(correct.equals("d")){
					score = score + 5;
				}
				
				final Context c = v.getContext();
	            
			    final Intent intent = new Intent(c, SinglePlay.class);
			    final Intent safe = new Intent(c, Score.class);
	            // I am sending the APP_ID and the friends 
			    // on the other side to connect both
			    // and the userID
	            intent.putExtra("api", app_id);
	            intent.putExtra("questions", rest);
	            intent.putExtra("myID", myID);
	            intent.putExtra("score", score);
	            
	            safe.putExtra("api", app_id);
	            safe.putExtra("myID", myID);
	            safe.putExtra("score", score);
	            // Put the dialog in a new thread so that it can close 
	            // properly afterwards
	            new Thread(new Runnable(){
	            	public void run(){
	            		finish();
	            		if(rest.length > 0)
	            			startActivity(intent);
	            		else{
	            			startActivity(safe);
	            		}
	            		progress.dismiss();
	            	}
	            	}).start();
			}
	    	 
	     });
	}

}
